<?php
/**
 * Created by JetBrains PhpStorm.
 * File: AccessAction.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-18
 * Time: 下午7:11
 */

	class AccessAction extends CommonAction{

		//节点列表
		function index(){

			$node = M('Node');
			$access = M('Access');
			import("ORG.Util.page");

				if(!empty($_GET['rid']) && isset($_GET['rid'])){
					$datas['level'] = 1;
					$datas['pid'] = 0;
					$datas['role_id'] = $_GET['rid'];
					$info = $access->where($datas)->getField('node_id,role_id');
					unset($datas['role_id']);
					$count = $node->where($datas)->count();
					if($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $node->where($datas)->limit($page->getlimitfirst().','.C('PAGENUM'))->select();
					foreach($list as $key=>$value){
						if(array_key_exists($value['id'], $info)){
							$list[$key]['acc'] = "已授权";
						}else{
							$list[$key]['acc'] = "未授权";
						}
						$num = $node->where('pid='.$value['id'])->count();
						$list[$key]['count'] = $num;
					}
					if($count==0){
						$this->assign('isshow', 'no');
						$this->assign('nobody', 'no');
					}else{
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');
					}
				}else{
					$this->error('异常操作');
				}
			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();
		}

		function modelindex(){

			$node = M('Node');
			$access = M('Access');
			import("ORG.Util.page");

				if(isset($_GET['rid']) && !empty($_GET['rid'])){
					$datas['pid'] = $_GET['pid'];
					$datas['level'] = 2;
					$datas['role_id'] = $_GET['rid'];
					$info = $access->where($datas)->getField('node_id,role_id');
					unset($datas['role_id']);
					$count = $node->where($datas)->count();
					if($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $node->where($datas)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
					foreach($list as $key=>$value){
						if(array_key_exists($value['id'], $info)){
							$list[$key]['acc'] = "已授权";
						}else{
							$list[$key]['acc'] = "未授权";
						}
						$num = $node->where('pid='.$value['id'])->count();
						$list[$key]['count'] = $num;
					}

					if($count==0){
						$this->assign('isshow', 'no');
						$this->assign('nobody', 'no');
					}else{
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');
					}
				}else{
					$this->error('异常操作');
				}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();
		}

		function actionindex(){

			$node = M('Node');
			$access = M('Access');
			import("ORG.Util.page");

			if(isset($_GET['rid']) && !empty($_GET['rid'])){
				$datas['pid'] = $_GET['pid'];
				$datas['level'] = 3;
				$datas['role_id'] = $_GET['rid'];
				$info = $access->where($datas)->getField('node_id,role_id');
				unset($datas['role_id']);
				$count = $node->where($datas)->count();
				if($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
				$list = $node->where($datas)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
				foreach($list as $key=>$value){
					if(array_key_exists($value['id'], $info)){
						$list[$key]['acc'] = "已授权";
					}else{
						$list[$key]['acc'] = "未授权";
					}
				}

				if($count==0){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}else{
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}else{
				$this->error('异常操作');
			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();
		}

		//添加菜单
		function insert(){
			$node = M('Node');
			$access = M('Access');
			if(isset($_POST['rid']) && !empty($_POST['rid'])){
				if(isset($_POST['nid']) && !empty($_POST['nid'])){

					$nid = implode(',', $_POST['nid']);

					$datas = $node->where('id IN ('.$nid.')')->select();

					foreach($datas as $key){
						$list['node_id'] = $key['id'];
						$list['pid'] = $key['pid'];
						$list['level'] = $key['level'];
						$list['role_id'] = $_POST['rid'];

						$info = $access->where('role_id='.$list['role_id'])->getField('node_id,role_id');
						$pinfo = $access->where('role_id='.$list['role_id'].' AND node_id='.$list['pid'])->find();

				//		$this->assign('waitSecond',9999);

							if(empty($pinfo) && $list['pid']!=0){
								$this->error('请先授权上级节点');
							}else{
								if(array_key_exists($list['node_id'], $info)){
									$this->error('该节点已经授权');
								}else{
									if($access->add($list)){
										$flag = true;
									}else{
										$this->error('授权失败');
									}
								}
							}

					}
					if($flag){
						$this->success('授权成功');
					}
				}else{
					$this->error('请选择节点');
				}
			}else{
				$this->error('异常操作');
			}

		}

		//删除节点
		function del(){
			if(isset($_POST['rid']) && !empty($_POST['rid'])){
				if(isset($_POST['nid']) && !empty($_POST['nid'])){
					$access = D('Access');
					foreach($_POST['nid'] as $key=>$value){
						$exists = $access->where('pid='.$value)->select();
						$info = $access->where('node_id='.$value.' AND role_id='.$_POST['rid'])->select();
						if(!empty($exists)){
							$this->error('该节点下还有子节点没有删除');
						}elseif(empty($info)){
							$this->error('该节点没有被授权');
						}else{
							$num = $access->where('node_id='.$value)->delete();
							if($num>0){
								$this->success('取消授权成功');
							}else{
								$this->error('取消授权失败');
							}
						}
						dump($exists);
						dump($info);
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('请选择节点');
			}
		}


		}